﻿package view.popup
{
    import cmd.*;
    import com.core.*;
    import com.core.display.*;
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import consts.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.text.*;
    import manager.*;
    import uiresource.popup.*;

    public class SureBayDialogCtrl extends NewSureBayDialogUI implements IDialogUI
    {
        private var amounttext:TextField;
        private var allpricetext:TextField;
        private var item:Loader;
        private var curCount:int = 1;
        private var _Index:int;
        public var data:Object;
        private var Max:int;
        private var background:Sprite;
        private var cfgShop:Object;

        public function SureBayDialogCtrl()
        {
            this.cfgShop = ConfigDictionary.data.shop;
            return;
        }// end function

        override protected function createChildren() : void
        {
            createView(uiXML);
            this.init();
            return;
        }// end function

        override protected function initialize() : void
        {
            super.initialize();
            return;
        }// end function

        public function init() : void
        {
            var _loc_1:TextFormat = null;
            this.background = new Sprite();
            this.background.graphics.beginFill(0, 0.5);
            this.background.graphics.drawRect(0, 0, GlobalLayer.instance.root.stage.stageWidth, GlobalLayer.instance.root.stage.stageHeight);
            this.background.visible = false;
            this.background.addEventListener(MouseEvent.MOUSE_DOWN, this.onDown);
            PluginManager.instance.alertContainer.addChild(this.background);
            this.visible = false;
            this.x = GlobalLayer.instance.root.stage.stageWidth - this.width >> 1;
            this.y = GlobalLayer.instance.root.stage.stageHeight - this.height >> 1;
            _loc_1 = new TextFormat("宋体", 12);
            _loc_1.align = TextFormatAlign.CENTER;
            this.amounttext = new TextField();
            this.amounttext.x = 155;
            this.amounttext.y = 145;
            this.amounttext.width = 72;
            this.amounttext.height = 20;
            this.amounttext.defaultTextFormat = _loc_1;
            this.amounttext.text = String(this.curCount);
            this.amounttext.textColor = MColor.V_NORMAL;
            this.amounttext.type = TextFieldType.INPUT;
            this.amounttext.maxChars = 10;
            this.amounttext.restrict = "0-9";
            this.amounttext.addEventListener(MouseEvent.CLICK, this.inputClickHandler);
            this.amounttext.addEventListener(Event.CHANGE, this.inputChangeHandler);
            this.addChild(this.amounttext);
            this.allpricetext = new TextField();
            this.allpricetext.x = 132;
            this.allpricetext.y = 181;
            this.allpricetext.width = 170;
            this.allpricetext.height = 20;
            this.allpricetext.defaultTextFormat = _loc_1;
            this.allpricetext.textColor = MColor.V_NORMAL;
            this.allpricetext.autoSize = TextFieldAutoSize.CENTER;
            this.addChild(this.allpricetext);
            leftBtn.addEventListener(MouseEvent.CLICK, this.onLeft);
            rightBtn.addEventListener(MouseEvent.CLICK, this.onRight);
            sureBtn.label = "确认";
            sureBtn.labelFont = "宋体";
            sureBtn.labelColors = "0xb29a6c, 0xb29a6c, 0xb29a6c";
            sureBtn.addEventListener(MouseEvent.CLICK, this.onSure);
            cancelBtn.label = "取消";
            cancelBtn.labelFont = "宋体";
            cancelBtn.labelColors = "0xb29a6c, 0xb29a6c, 0xb29a6c";
            cancelBtn.addEventListener(MouseEvent.CLICK, this.onCancel);
            maxBtn.addEventListener(MouseEvent.CLICK, this.onMax);
            closeBtn.addEventListener(MouseEvent.CLICK, this.onClose);
            this.addEventListener(MouseEvent.CLICK, this.onStageClicked);
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.onDown);
            GlobalLayer.instance.root.addEventListener(Event.RESIZE, this.resizeHandler);
            return;
        }// end function

        private function inputClickHandler(event:MouseEvent) : void
        {
            GlobalLayer.instance.flashStage.focus = this.amounttext;
            event.stopImmediatePropagation();
            return;
        }// end function

        private function inputChangeHandler(event:Event) : void
        {
            this.curCount = int(this.amounttext.text);
            this.changeNumText(this.curCount);
            return;
        }// end function

        private function onLeft(event:MouseEvent) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this.curCount - 1;
            _loc_2.curCount = _loc_3;
            if (this.curCount < 1)
            {
                this.curCount = 1;
            }
            this.changeNumText(this.curCount);
            return;
        }// end function

        private function onRight(event:MouseEvent) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this.curCount + 1;
            _loc_2.curCount = _loc_3;
            if (this.curCount >= 99)
            {
                this.curCount = 99;
            }
            this.changeNumText(this.curCount);
            return;
        }// end function

        private function onMax(event:MouseEvent) : void
        {
            ModelProxy.change("guide", "hide", GuideType.GUIDE_BB3);
            this.changeNumText(99);
            return;
        }// end function

        private function onStageClicked(event:MouseEvent) : void
        {
            GlobalLayer.instance.flashStage.focus = null;
            return;
        }// end function

        private function changeNumText(param1:int) : void
        {
            var _loc_3:int = 0;
            var _loc_2:* = ModelProxy.getValue("data", "bag.gold") + ModelProxy.getValue("data", "bag.bgold");
            if (this.data)
            {
                _loc_3 = int(_loc_2 / this.data.sellvalue);
                if (_loc_3 > this.Max)
                {
                    _loc_3 = this.Max;
                }
                if (param1 > _loc_3)
                {
                    param1 = _loc_3;
                }
            }
            this.amounttext.text = String(param1);
            this.curCount = param1;
            if (this.data == null)
            {
                this.allpricetext.text = "";
                return;
            }
            this.allpricetext.text = String(param1 * int(this.data.sellvalue));
            return;
        }// end function

        private function onSure(event:MouseEvent) : void
        {
            var _loc_3:int = 0;
            var _loc_4:Object = null;
            var _loc_2:* = int(this.amounttext.text);
            if (_loc_2 == 0)
            {
                return;
            }
            _loc_3 = 0;
            for each (_loc_4 in this.cfgShop)
            {
                
                if (int(_loc_4.itemid) == this.data.itemid)
                {
                    _loc_3 = int(_loc_4.id);
                    break;
                }
            }
            PluginManager.instance.sendToServer("shop", CMDShop.Bay, {id:_loc_3, count:_loc_2, auto:this.data.itemid});
            this.close();
            return;
        }// end function

        private function onCancel(event:MouseEvent) : void
        {
            ModelProxy.change("data", "digflag", false);
            this.close();
            return;
        }// end function

        public function show(param1:Object) : void
        {
            var _loc_3:Object = null;
            var _loc_4:int = 0;
            if (!param1)
            {
                return;
            }
            this.data = param1;
            this.darwItem();
            label_itemname.text = this.data.name;
            this.allpricetext.text = String(this.curCount * int(this.data.sellvalue));
            var _loc_2:int = 0;
            for each (_loc_3 in this.cfgShop)
            {
                
                if (_loc_3.itemid == this.data.itemid)
                {
                    _loc_2 = _loc_3.costtype;
                }
            }
            _loc_4 = 0;
            if (_loc_2 == 1)
            {
                yuanbaoImg.url = "png.common.cs2_jinbi";
                _loc_4 = UserData.getBagData("gold") + UserData.getBagData("bgold");
            }
            else if (_loc_2 == 2)
            {
                yuanbaoImg.url = "png.common.cs2_qian";
                _loc_4 = UserData.getBagData("bonus");
            }
            var _loc_5:* = int(_loc_4 / this.data.sellvalue);
            var _loc_6:int = 1;
            var _loc_7:int = 99;
            if (this.data.itemtype == 1)
            {
                _loc_5 = 1;
                _loc_7 = 1;
            }
            else if (_loc_5 > 999)
            {
                _loc_5 = 999;
            }
            if (_loc_7 > _loc_5)
            {
                _loc_7 = _loc_5;
            }
            if (_loc_5 == 0)
            {
                _loc_6 = 0;
            }
            this.Max = _loc_5;
            label_money.text = this.data.sellvalue;
            label_money1.text = String(this.Max);
            var _loc_8:Boolean = true;
            this.visible = true;
            this.background.visible = _loc_8;
            return;
        }// end function

        private function darwItem() : void
        {
            if (this.item)
            {
                this.item.parent.removeChild(this.item);
            }
            var imgURL:* = new URLRequest();
            imgURL.url = Config.getUrl("assets/images/icon/tools/" + this.data.icon + ".png", ResourceType.NORMAL);
            this.item = new Loader();
            this.item.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function () : void
            {
                return;
            }// end function
            );
            this.item.load(imgURL);
            this.item.x = 59 - 28;
            this.item.y = 79 - 22;
            this.addChild(this.item);
            return;
        }// end function

        override public function set visible(param1:Boolean) : void
        {
            super.visible = param1;
            return;
        }// end function

        private function onClose(event:MouseEvent) : void
        {
            ModelProxy.change("data", "digflag", false);
            this.close();
            return;
        }// end function

        public function close() : void
        {
            var _loc_1:Boolean = false;
            this.visible = false;
            this.background.visible = _loc_1;
            this.curCount = 1;
            this.amounttext.text = this.curCount.toString();
            this.allpricetext.text = String(this.curCount * int(this.data.sellvalue));
            this.data = null;
            return;
        }// end function

        private function onDown(event:MouseEvent) : void
        {
            event.stopImmediatePropagation();
            return;
        }// end function

        private function resizeHandler(event:Event) : void
        {
            this.background.width = GlobalLayer.instance.root.stage.stageWidth;
            this.background.height = GlobalLayer.instance.root.stage.stageHeight;
            this.x = GlobalLayer.instance.root.stage.stageWidth - this.width >> 1;
            this.y = GlobalLayer.instance.root.stage.stageHeight - this.height >> 1;
            return;
        }// end function

    }
}
